﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NotebookPortal
{
    public partial class selectorder : System.Web.UI.Page
    {


        void BindPriceList()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();

            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT * FROM PRICELIST";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);



            ASPxComboBox1.TextField = "PRICELISTNAME";
            ASPxComboBox1.ValueField = "PRICELISTCODE";

            ASPxComboBox1.DataSource = dt;
            ASPxComboBox1.DataBind();
            oConnection.Close();

        }


        void BindWarehouse()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();

            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT * FROM WAREHOUSE";
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);


            cboWarehouse.TextField = "Dscription";
            cboWarehouse.ValueField = "Code";

            cboWarehouse.DataSource = dt;
            cboWarehouse.DataBind();

            oConnection.Close();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDraftSONum.Text = Session["DraftSONum"].ToString();
                BindPriceList();
                BindWarehouse();
            }

            if (ViewState["AvailableStocks"] != null)
            {
                grid.DataSource = (DataTable)ViewState["AvailableStocks"];
                grid.DataBind();
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT *, cast(0 as bit) IsFree FROM VW_AVAILABLESTOCKS WHERE PriceListCode=@PriceListCode AND WhsCode=@WhsCode";
            oCommand.Parameters.Add(new SqlParameter("@PriceListCode", ASPxComboBox1.Value.ToString()));
            oCommand.Parameters.Add(new SqlParameter("@WhsCode", cboWarehouse.Value.ToString()));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);
            oConnection.Close();

            if (ViewState["AvailableStocks"] != null)
            {
                ViewState["AvailableStocks"] = dt;
            }
            else
            {
                ViewState.Add("AvailableStocks", dt);
            }

            grid.DataSource = (DataTable)ViewState["AvailableStocks"];
            grid.DataBind();
        }

        protected void txtOrderQty_ButtonClick(object source, DevExpress.Web.ASPxEditors.ButtonEditClickEventArgs e)
        {
            DevExpress.Web.ASPxEditors.ASPxButtonEdit edtQty = (DevExpress.Web.ASPxEditors.ASPxButtonEdit)source;

            DevExpress.Web.ASPxGridView.GridViewPreviewRowTemplateContainer oPreviewRow = (DevExpress.Web.ASPxGridView.GridViewPreviewRowTemplateContainer)edtQty.NamingContainer;

            int iVisibleIndex = oPreviewRow.VisibleIndex;

            DevExpress.Web.ASPxEditors.ASPxLabel oLblPrice = (DevExpress.Web.ASPxEditors.ASPxLabel)oPreviewRow.FindControl("lblUnitPrice");
            DevExpress.Web.ASPxEditors.ASPxLabel oLblUntMsr = (DevExpress.Web.ASPxEditors.ASPxLabel)oPreviewRow.FindControl("lblUoM");
            DevExpress.Web.ASPxEditors.ASPxCheckBox oChkIsFree = (DevExpress.Web.ASPxEditors.ASPxCheckBox)oPreviewRow.FindControl("chkIsFree");


            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();

            string sItemCode = grid.GetRowValues(iVisibleIndex, "ItemCode").ToString();
            string sItemName = grid.GetRowValues(iVisibleIndex, "ItemName").ToString();


            #region Adding Item in the Database

            string _NewGUID = System.Guid.NewGuid().ToString();

            oConnection = new SqlConnection();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oConnection.Open();

            oCommand = new SqlCommand();
            oCommand.Connection = oConnection;

            oCommand.CommandText = "INSERT INTO RDR1 (DocNum, ItemCode, ItemDesc, UoM, Qty, CQty, UnitPrice, PriceListCode, IsFree, DiscPrcnt, Discount, TotalLineDisc, Amount, ItemGuID) VALUES " +
                                                            "(@DocNum, @ItemCode, @ItemDesc, @UoM, @Qty, @CQty, @UnitPrice, @PriceListCode, @IsFree, @DiscPrcnt, @Discount, @TotalLineDisc, @Amount, @ItemGuID)";

            oCommand.Parameters.Add(new SqlParameter("@ItemGuID", _NewGUID));
            oCommand.Parameters.Add(new SqlParameter("@DocNum", txtDraftSONum.Text));
            oCommand.Parameters.Add(new SqlParameter("@ItemCode", sItemCode));
            oCommand.Parameters.Add(new SqlParameter("@ItemDesc", sItemName));
            oCommand.Parameters.Add(new SqlParameter("@UoM", oLblUntMsr.Text.ToString()));
            oCommand.Parameters.Add(new SqlParameter("@Qty", Convert.ToDouble(edtQty.Text)));
            oCommand.Parameters.Add(new SqlParameter("@CQty", 1));
            oCommand.Parameters.Add(new SqlParameter("@PriceListCode", ASPxComboBox1.Value.ToString()));
            
            
            if (Convert.ToBoolean(oChkIsFree.Checked) == false)
            {
                oCommand.Parameters.Add(new SqlParameter("@UnitPrice", Convert.ToDouble(oLblPrice.Text)));
            }
            else
            {
                oCommand.Parameters.Add(new SqlParameter("@UnitPrice", 0));
            }
            oCommand.Parameters.Add(new SqlParameter("@IsFree", oChkIsFree.Checked));

            double dZeroValue = 0;
            oCommand.Parameters.Add(new SqlParameter("@DiscPrcnt", dZeroValue));
            oCommand.Parameters.Add(new SqlParameter("@Discount", dZeroValue));
            oCommand.Parameters.Add(new SqlParameter("@TotalLineDisc", dZeroValue));
            if (Convert.ToBoolean(oChkIsFree.Value) == false)
            {
                oCommand.Parameters.Add(new SqlParameter("@Amount", Convert.ToDouble(edtQty.Text) * Convert.ToDouble(oLblPrice.Text)));
            }
            else
            {
                oCommand.Parameters.Add(new SqlParameter("@Amount", 0));
            }

            oCommand.ExecuteNonQuery();

            oConnection.Close();
            #endregion

            edtQty.Text = "0";
            oChkIsFree.Value = false;
        }

    }
}